<?php 
class ImportCtrl extends jController {

     /**
    * Affiche la page de liste des mail
    */
    function index() {
        $rep = $this->getResponse('html');
        $rep->menusActifs = adminHtmlResponse::MENU_ANNONCES ;


        jClasses::inc('annonce~listeImportSrv');
        $liste = listeImportSrv::liste();
        $t = $ret = array();
        foreach ($liste as $i=>$item){
            $occas[] = $item->IdOccasion;
            $orig[]  = $item->Occas_IDorigine;
            if ($item->critere != $liste[$i+1]->critere) {
                //if (sizeof($t) > 1) {
                    $o = new stdClass();
                    $o->cri = $item->critere;
                    $o->origine  = $orig;
                    $o->occasion = $occas;
                    $ret[] = $o;
                //}
                unset($orig);
                unset($occas);
                $occas = $orig = array();
                
            }
        }

        $tab = $tempDelete = $temp =  array();
        if (sizeof($ret)) {
            foreach ($ret as $item){
                if (sizeof($item->origine)) {
                    $tempOrigine = array_unique($item->origine);
                    if (sizeof($tempOrigine) == 1) {
                        // to delete
                        foreach ($item->occasion as $toDelete) {
                            if($toDelete != $item->occasion[0])
                                $tempDelete[] = $toDelete;
                        }
                    } else {
                        // s'il a +ieus partenaire ayant mm information : proirisé AO, AJA, etc ...
                        foreach ($item->origine as $key=>$origineDiff) {
                            // si AO
                            if (isset($origineDiff)) {
                                $occasId[]= $item->occasion[$key]; //mettre dans un tableau temporaire les idOccasions à comparer avant de faire la suppression des doublons 
                                $origine[]  = $item->origine[$key]; 
                                //élimine les autres partenaires autre que AJA
                                foreach($origine as $k=>$value){
                                    if($value == 'AJA'){
                                        $keepOccasIdPartenaire[] = $occasId[$k];
                                        $keepOriginePartenaire[] = $origine[$k];
                                    } else {
                                        $tempDeleteOccasIdPartenaire[] = $occasId[$k];
                                        $tempDeleteOriginePartenaire[] = $origine[$k];
                                    }
                                }

                            } else {
                                $keepOccasion[] = $item->occasion[$key];
                                $keepOrigine[]  = $item->origine[$key];
                            }
                        }
                        $keepOccasId        =  array_unique(array_merge($keepOccasIdPartenaire, $keepOccasion)); //tableau des IdOccasion ayant pour origine AJA et  vide 
                        $keepOrigineId      =  array_unique(array_merge($keepOriginePartenaire, $keepOrigine));
                        $tempDeleteAJA      =  listeImportSrv::AnnonceJauneToDelete($keepOccasId);  //afficher les annonces jaunes (AJA) à effacer 
                    }
                }
            }
               $tempDelete = array_unique(array_merge($tempDelete, $tempDeleteAJA,$tempDeleteOccasIdPartenaire));
        }
        listeImportSrv::deleteDoublon($tempDelete);
      }

}